package com.lagou.edu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @PostMapping(value = "/loginAction")
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        HttpSession session) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "login";
        }
        if ("admin".equals(userName) && "admin".equals(password)) {
            session.setAttribute("loginUser", userName);
            session.setAttribute("loginUserId", userName);
            //session过期时间设置为7200秒 即两小时
            //session.setMaxInactiveInterval(60 * 60 * 2);
            return "resume/index";
        } else {
            session.setAttribute("errorMsg", "登陆失败，用户名或密码错误");
            return "login";

        }
    }

    @GetMapping({"/index", "/", "/index.html"})
    public String index(){
        return "login";
    }

    @GetMapping({"/resume/index", "/resume", "/resume/index.html"})
    public String resume(){
        return "resume/index";
    }
}
